package com.itheima.service.ServiceImpl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.AdminOderMapper;
import com.itheima.pojo.AdminOder;
import com.itheima.pojo.AdminOderQueryParam;
import com.itheima.pojo.PageBean;
import com.itheima.service.AdminOrderService;
import com.itheima.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

import static java.time.LocalTime.now;

@Service
public class AdminOrderServiceImpl implements AdminOrderService {
    @Autowired
    private AdminOderMapper adminOderMapper;
    @Autowired
    private HttpServletRequest request;
    @Override
    public List<AdminOder> list() {
       List<AdminOder>lists= adminOderMapper.list();
       return lists;
    }

    @Override
    public AdminOder getById(Integer id) {
        AdminOder adminOder =   adminOderMapper.getById();
         return adminOder;
    }

    @Override
    public void update(AdminOder adminOder) {
        String token = request.getHeader("token");
        Claims claims = JwtUtils.parseJWT(token);
        Long id =  (Long)claims.get("id");
        adminOder.setUpdateBy(id);
        adminOder.setUpdateTime(LocalDateTime.now());
        adminOderMapper.update(adminOder);
    }

    @Override
    public PageBean page(AdminOderQueryParam param) {
        PageHelper.startPage(param.getPage(),param.getPageSize());
        Page<AdminOder> pages=adminOderMapper.page(param);
        return new PageBean(pages.getTotal(),pages.getResult());
    }

    @Override
    public void delete(Integer id) {
        adminOderMapper.deleteById(id);
    }

    @Override
    public void add(AdminOder adminOder) {
        adminOder.setCreateTime(LocalDateTime.now());
        adminOderMapper.add(adminOder);
    }


}
